package cn.com.utt.unms.mapper;

import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import cn.com.utt.unms.pojo.EventLog;

@Mapper
public interface EventLogMapper {
	
	@Insert("insert into event_log(sn,type,stamp,content) values(#{sn},#{type},now(),#{content})")
	void save(@Param("sn") String sn,@Param("type") int type,@Param("content") String content);
	
	@Select("select type,count(id) as count from event_log where stamp>#{stamp} group by type")
	List<Map<String, Object>> queryEventLogByType(@Param("stamp") String stamp);
	
	@Select({"<script>",
	    "SELECT type,count(id) as count ", 
	    "FROM event_log",
	    "WHERE ", 
	    "stamp>#{stamp} and",
	    "sn IN", 
	      "<foreach item='item' index='index' collection='sn'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	      "group by type",
	"</script>"})
	List<Map<String, Object>> queryEventLogByTypeAndSn(@Param("sn")String[] sn,@Param("stamp") String stamp);
	
	@Select({"<script>",
	    "SELECT *", 
	    "FROM event_log",
	    "WHERE 1=1",
	    "<if test='sns != null and sns.size>0'>",
	      "and sn IN",
	      "<foreach item='item' index='index' collection='sns'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	      "</if>",
	      "<if test='searchs != null'>",
	      "<foreach item='item' index='index' collection='searchs'",
	        "open='and' separator='and' close=''>",
	        "<choose>",
	        "<when test='item.op != \"like\"'>",
	        "${item.column}",
	        " = ",
	        "#{item.value}",
	        "</when>",
	        "<otherwise>",
	        "${item.column}",
	        " like ",
	        "'%${item.value}%'",
	        "</otherwise>",
	        "</choose>",
	      "</foreach>",
	      "</if>",
	      "<if test='orderColumn != null'>",
	      "order by ${orderColumn} ${orderDirection}",
	      "</if>",
	      "limit #{start},#{rows}",
	    "</script>"})
	List<EventLog> queryByCondition(Map<String, Object> conditions);
	
	@Select({"<script>",
	    "SELECT * ", 
	    "FROM event_log",
	    "WHERE ", 
	    "stamp>#{stamp} and",
	    "sn IN", 
	      "<foreach item='item' index='index' collection='sn'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	      "order by stamp desc limit 10",
	"</script>"})
	List<EventLog> queryEventLogs(@Param("sn")String[] sn,@Param("stamp") String stamp);
	
	
	@Select({"<script>",
	    "SELECT COUNT(*) AS total", 
	    "FROM event_log",
	    "WHERE 1=1",
	    "<if test='sns!=null and sns.size>0'>",
	     "and sn in",
	    	"<foreach item='item' index='index' collection='sns'",
	    	"open='(' separator=',' close=')'>",
	    	"#{item}",
	    	"</foreach>",
	    "</if>",
		      "<if test='searchs != null'>",
		      "<foreach item='item' index='index' collection='searchs'",
		        "open='and' separator='and' close=''>",
		        "<choose>",
		        "<when test='item.op != \"like\"'>",
		        "${item.column}",
		        " = ",
		        "#{item.value}",
		        "</when>",
		        "<otherwise>",
		        "${item.column}",
		        " like ",
		        "'%${item.value}%'",
		        "</otherwise>",
		        "</choose>",
		      "</foreach>",
		      "</if>",
	    "</script>"})
	int countByCondition(Map<String, Object> conditions);
	
	@Delete({"<script>",
	    "delete from event_log", 
	    "WHERE ", 
	    "sn IN", 
	      "<foreach item='item' index='index' collection='sn'",
	        "open='(' separator=',' close=')'>",
	        "#{item}",
	      "</foreach>",
	"</script>"})
	void delete(@Param("sn")List<String> sn);
	
}
